/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.poligran.polla.mundialista.ingesoft.impl;

import co.com.poligran.polla.mundialista.ingesoft.base.Sala;
import co.com.poligran.polla.mundialista.ingesoft.base.Usuario;
import co.com.poligran.polla.mundialista.ingesoft.dao.SalaDAO;
import co.com.poligran.polla.mundialista.ingesoft.dao.UsuarioDAO;
import co.com.poligran.polla.mundialista.ingesoft.db.Entitie;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author alejo
 */
public class SalaImpl extends Entitie implements SalaDAO {

    public static void main(String[] args) {
        SalaDAO dAO = new SalaImpl();
        //Sala u = new Sala(2, "mundial de piquies");
      //System.out.println(dAO.insert(u));
        for (Sala un : dAO.selectAll()) {
            System.out.println(un);
        }
        
        //System.out.println("Existe: " + dAO.select(4));
        System.out.println("Delete: " + dAO.delete(3));
       // System.out.println("Update: " + dAO.update(u));

    }

    @Override
    public boolean insert(Sala e) {
        boolean inserto = false;
        try {
            this.cs = conn.prepareCall("EXEC sp_InsertSala ?");

            this.cs.setString(1, e.getNombre());

            this.cs.executeUpdate();

            inserto = true;

        } catch (SQLException ex) {
            System.out.print("exepction  " + ex.getMessage());
        } finally {
            if (this.cs != null) {
                try {
                    this.cs.close();
                } catch (SQLException ex) {
                    System.out.print("exepction  " + ex.getMessage());
                }
            }
        }
        return inserto;
    }

    @Override
    public Sala select(int idEntitie) {
        Sala selectSala = null;
        try {
            this.query = "EXEC sp_GetSala ?";
            this.cs = conn.prepareCall(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.cs.setInt(1, idEntitie);
            this.rs = cs.executeQuery();
            if (rs.next()) {
                selectSala = new Sala();
                selectSala.setIdSala(rs.getInt(1));
                selectSala.setNombre(rs.getString(2));
            }
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException sqlers) {
                System.out.println(sqlers.getMessage());
            }
        }
        return selectSala;
    }

    @Override
    public LinkedList<Sala> selectAll() {
        LinkedList<Sala> selectSalas = null;
        try {
            this.query = "select * from sala";
            this.ps = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.rs = ps.executeQuery();
            selectSalas = new LinkedList();
            while (rs.next()) {
                Sala sala = new Sala();
                sala.setIdSala(rs.getInt(1));
                selectSalas.add(sala);
            }
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException sqlers) {
                System.out.println(sqlers.getMessage());
            }
        }
        return selectSalas;
    }

    @Override
    public boolean update(Sala e) {
        boolean update = false;
        try {
            this.cs = conn.prepareCall("EXEC sp_UpdateSala ?,?");

            this.cs.setInt(1, e.getIdSala());
            this.cs.setString(2, e.getNombre());

            // call executeUpdate
            if (this.cs.executeUpdate() == 1) {
                update = true;
            }

        } catch (SQLException ex) {
            System.out.print("exepction  " + ex.getMessage());
        } finally {
            if (this.cs != null) {
                try {
                    this.cs.close();
                } catch (SQLException ex) {
                    System.out.print("exepction  " + ex.getMessage());
                }
            }
        }
        return update;
    }

    @Override
    public boolean delete(int idEntitie) {
        boolean resp = false;
        try {
            this.query = "EXEC sp_DeleteSala ?";
            this.cs = conn.prepareCall(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            // set preparedstatement 
            this.cs.setInt(1, idEntitie);
            // call executeUpdate
            if (this.cs.executeUpdate() == 1) {
                resp = true;
            }
        } catch (SQLException se) {
            System.out.print("exepction  " + se.getMessage());
        } finally {
            try {
                this.cs.close();
            } catch (SQLException ex) {
                System.out.print("exepction  " + ex.getMessage());
            }
        }
        return resp;
    }
}
